<?php
/*****************************************************

  Copyright:	(c) 2006  SeaDesign Web Studio
        Web:	www.seadesign.ru
      Email: 	seadesign@gmail.com
  
*****************************************************/

require_once('data.inc');

class ValueProcessor {
	function process(&$val) {
		return $val;
	}
};

class HTMLProcessor extends ValueProcessor {
	function process(&$val) {
		return htmlspecialchars($val, ENT_NOQUOTES, 'cp1251');
	}
};


class WordReplaceProcessor extends ValueProcessor {
	function WordReplaceProcessor($search, $replace) {
		$this->search = $search;
		$this->replace = $replace;
	}
	
	function process(&$val) {
		return str_replace($this->search, $this->replace, $val);
	}
	
//private:
	var $search, $replace;
};

class Grid 
{
	function Grid($url, $limitRecOnPage) {
		$this->url = $url;
		$this->limitRecOnPage = $limitRecOnPage;
		$this->keyFields = array();
		$this->columns = array();
		$this->valueProcessors = array();
		
		$this->addProcessor(new HTMLProcessor);
	}
	
	function addKeyField($posInSrc, $name, $type) {
		$this->keyFields[] = array(
			'pos' => $posInSrc,
			'name' => $name,
			'type' => $type
		);
	}
	
	function addColumn(
		$posInSrc,
		$name,
		$title,
		$cellTxt = '{VALUE}',	//may contain {VALUE} {KEYPARAMS}
		$size = 0)
	{
		$this->columns[] = 
			array(
				'pos' => $posInSrc,
				'name' => $name,
				'title' => $title,
				'size' => $size,
				'cellTxt' => $cellTxt
			);
	}
	
	function addFakeColumn(
		$title,
		$cellTxt = '{KEYPARAMS}',	//may contain {KEYPARAMS} {KEYVALUES}
		$size = 0)
	{
		$this->columns[] = 
			array(
				'title' => $title,
				'size' => $size,
				'cellTxt' => $cellTxt
			);
	}
	
	function create(&$q) {
		$this->q = $q;
	}
	
	function addProcessor($processor) {
		$this->valueProcessors[] = $processor;
	}
	
	function render($id=null) 
	{
		echo "<div class=grid";
		if ($id) {
			echo " id='$id'";
		}
		echo "><table width=100%>";
				
		echo "<tr class=head>";
		$this->renderHeader();
		echo "</tr>";
		
		$count=0;
		for (; $this->q->fetch() && $count<$this->limitRecOnPage; ++$count) {
			echo "<tr>";
			$this->renderCells( $this->getKeyParamString(), $this->getKeyValuesString() );
			echo "</tr>";
		}
		
		echo
				"</table>".
			"</div>";
		
		return $count;
	}
	
	
//protected:

	function renderCells($keyParams, $keyValues) { 
		foreach ($this->columns as $i => $params) {
			echo "<td>";
			if (isset($params['name'])) {
				$this->renderCell(
					$this->q->at($params['pos']), 
					$keyParams, 
					$keyValues,
					$params['cellTxt'], 
					$params['name']
				);
			}
			else {
				$this->renderFakeCell($keyParams, $keyValues, $params['cellTxt']);
			}
			echo "</td>";
		}
	}
	
	function renderCell($val, $keyParams, $keyValues, $cellTxt, $name) 
	{
		foreach ($this->valueProcessors as $pr) {
			$val = $pr->process($val);
		}
		
		echo 
			str_replace(
				array('{VALUE}', '{KEYPARAMS}', '{KEYVALUES}'), 
				array($val, $keyParams, $keyValues), 
				$cellTxt
			);
	}
	
	function renderFakeCell($keyParams, $keyValues, $cellTxt) 
	{
		echo 
			str_replace(
				array('{KEYPARAMS}', '{KEYVALUES}'),
				array($keyParams, $keyValues),
				$cellTxt
			);
	}

	
	function renderHeader() {
		foreach ($this->columns as $i => $params) {
			$this->renderHeaderColumn(
				$params['title'],
				$params['size'],
				isset($params['name']) ? $params['name'] : null
			);
		}
	}
	
	function renderSort($name, &$sortOp, &$sortImg) {
		$sort = ASC;
		if ($this->q->hasOrderBy($name)) {
			$sort = $this->q->getOrderBy($name);
			if ($sort == ASC) {
				$sort = DESC;
				$sortImg = "<img width=10 height=6 style='margin-right:2px;' src='img/asc.gif'/>";
			}
			else {
				$sort = ASC;
				$sortImg = "<img width=10 height=6 style='margin-right:2px;' src='img/desc.gif'/>";
			}
		}
		$sortOp = " onclick=window.location.href=\"{$this->url}sort=$name=$sort@\" ";
	}
	
	function renderHeaderColumn($title, $size, $name) 
	{
		$sortOp = $sortImg = '';
		
		if ($title == 'del') {
			echo "<td><input type=checkbox class='checkall' onclick=\"isChecked=this.checked; $('.grid :checkbox').each(function() { $(this).attr('checked',isChecked); } )\"/></td>";
			return;
		}
		
		if ($name) {
			$this->renderSort($name, $sortOp, $sortImg);
		}
		
		if ($size) {
			echo "<td title='$title' width='$size' $sortOp>$sortImg";
		}
		else {
			echo "<td title='$title' $sortOp>$sortImg";
		}
		
		if ($name && $this->q->hasFilterBy($name)) {
			echo '*';		//filter sign
		}
		
		echo $title;
		echo "</td>";
	}

	function getKeyParamString() {
		$strParam = '';
		foreach ($this->keyFields as $i => $params) {
			if (strlen($strParam)) {
				$strParam .= '@';
			}
			$strParam .= 
				$this->getKeyFieldNameValString(
					$params['name'], 
					$this->q->at($params['pos']),
					$params['type']
				);
		}
		return $strParam;
	}
	
	function getKeyFieldNameValString($name, $val, $type) {
		return "$name=$val";
	}
	
	function getKeyValuesString()
	{
		$strParam = '';
		foreach ($this->keyFields as $i => $params) {
			if (strlen($strParam)) {
				$strParam .= '@';
			}
			$strParam .= 
				$this->getKeyFieldValString(
					$this->q->at($params['pos']),
					$params['type']
				);
		}
		return $strParam;
	}
	
	function getKeyFieldValString($val, $type) {
		return $val;
	}
	
//private:
	var $url;
	var $q;
	var $limitRecOnPage;
	var $keyFields;
	var $columns;
	
	var $valueProcessors;
};

?>